﻿namespace Whut.ObjectSharePointMapper.Internals
{
    using System;
    using System.Reflection;
    using Microsoft.SharePoint;

    public static class Utility
    {
        public static TAttribute GetAttribute<TAttribute>(this MemberInfo memberInfo) where TAttribute : Attribute
        {
            var attribute = Attribute.GetCustomAttribute(memberInfo, typeof(TAttribute));
            if (attribute == null)
            {
                return null;
            }

            return (TAttribute)attribute;
        }

        /// <summary>
        /// Returns list by relative url
        /// </summary>
        /// <param name="web">SPWeb from which take the lsit</param>
        /// <param name="listRelativeUrl">List relative url</param>
        /// <returns>List from given url</returns>
        public static SPList GetListByRelativeUrl(this SPWeb web, string listRelativeUrl)
        {
            return web.GetList(web.ServerRelativeUrl + listRelativeUrl);
        }
    }
}
